package com.sihan.swvp.action.jc;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.jc.JcBdz;
import com.sihan.swvp.service.jc.JcBdzService;
import com.sihan.swvp.service.jc.JcDsdwService;
import com.sihan.swvp.service.sys.LogService;

@Controller
@RequestMapping("/jc/JcBdzAction")
public class JcBdzAction extends BaseAction{

	@Autowired
	private JcBdzService jcBdzService;
	@Autowired
	private JcDsdwService jcDsdwService;
	@Autowired
	public LogService logService;
	@ResponseBody
	@RequestMapping("/getJcBdz")
	public Object getJcBdz(@Param("bdzCode")String bdzCode,@Param("bdzName")String bdzName,@Param("jcDsdwId")Integer jcDsdwId){
		try {
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("bdzCode", bdzCode);
			params.put("bdzName", bdzName);
			params.put("jcDsdwId", jcDsdwId);
			return jcBdzService.getJcBdz(params);
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常"); 
		}
	}
	/**
	 * 查询地市单位
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getJcDsdw")
	public Object getJcDsdw(@Param("dsdwName")String dsdwName){
		Map<String, Object> param=new HashMap<String, Object>();
		param.put("dsdwName", dsdwName);
		return jcDsdwService.getJcDsdw(param,null,null);
	}
	
	@ResponseBody
	@RequestMapping("/addJcBdz")
	public Object addJcBdz(JcBdz bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			boolean isDsdwNameOk=jcBdzService.getJcBdzValidate(null, bean.getBdzName(),null,bean.getJcDsdwId());
			boolean isCodeOk=jcBdzService.getJcBdzValidate(bean.getBdzCode(), null,null,bean.getJcDsdwId());
			if(isDsdwNameOk){
				return failMsg("变电站名称已存在");
			}else if(isCodeOk){
				return failMsg("变电站编码已存在");
			}
			jcBdzService.addJcBdz(bean);
			logService.addUserLog("添加变电站", "变电站", getLoginUser().getId(), "添加变电站："+bean.getBdzName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加变电站", "变电站", getLoginUser().getId(), "添加变电站："+bean.getBdzName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常"); 
		}
	}
	
	@ResponseBody
	@RequestMapping("/updateJcBdz")
	public Object updateJcBdz(JcBdz bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			boolean isDsdwNameOk=jcBdzService.getJcBdzValidate(null, bean.getBdzName(),bean.getId(),bean.getJcDsdwId());
			boolean isCodeOk=jcBdzService.getJcBdzValidate(bean.getBdzCode(), null,bean.getId(),bean.getJcDsdwId());
			if(isDsdwNameOk){
				return failMsg("变电站名称已存在");
			}else if(isCodeOk){
				return failMsg("变电站编码已存在");
			}
			jcBdzService.updateJcBdz(bean);
			logService.addUserLog("修改变电站", "变电站", getLoginUser().getId(), "修改变电站："+bean.getBdzName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("修改变电站", "变电站", getLoginUser().getId(), "修改变电站："+bean.getBdzName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常"); 
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteJcBdz")
	public Object deleteJcBdz(@Param("id")Integer id,String bdzName){
		try {
			jcBdzService.deleteJcBdz(id);
			logService.addUserLog("删除变电站", "变电站", getLoginUser().getId(), "删除变电站："+bdzName,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除变电站", "变电站", getLoginUser().getId(), "删除变电站："+bdzName,getRemoteAddr(),0,0);
			return failMsg("服务器异常"); 
		}
	}
}
